<?php
	require_once('header_sidebar.php');
	require_once('Class/rating.Class.php');
    require_once('Class/connexionDB.Class.php');

    $db = new connexionDB();
	$rating = new rating();	
?>
<aside class="right-side">
    <!-- Content Header (Page header) -->
    <?php if (isset($_GET['id'])) : ?>
	    <section class="content-header">
	        <h1>
	            <?php echo $translate->translate('HomePageRating'); ?>
	            <small>N&#176;<span id="id_rating"><?php echo $_GET['id']; ?></span></small>
	        </h1>
	        <ol class="breadcrumb">
	            <li><a href="#"><i class="fa fa-dashboard"></i><?php echo $translate->translate('HomePageRating'); ?></a></li>
	        </ol>
	    </section>
	    <section class="content">
	    	<?php 
                $id_rating = intval($_GET['id']);
	    		$ratingValues = $rating->getRatingFromID($id_rating);
                
                if (!empty($ratingValues))
                {
                    $countryName = $db->getCountryName($ratingValues['id_country']);
                    $recipientName = $db->getRecipientName($ratingValues['id_recipient']);
                    $topic = $db->getTopicName($ratingValues['id_topic']);
                    $criteria = $db->getCriteriaDescription($_GET['id']);
                    $markList = $rating->getOldMark($id_rating);
                    $debrief = $db->getDebrief($_GET['id']);
                }
    		?>
            <?php if (!empty($ratingValues)) : ?>
        		<div class="col-md-12 box box-info">
        			<div>
        				<h4 class="box-title">
    	                    <?php 
                                $title = $translate->translate('MultipleMark') . ' : ';
                                $title .= '<small><i>';

                                if (isset($markList) && $markList != '')
                                {
                                    $title .= $markList . ' - ';
                                }

                                $title .= '</i></small><b>'. $ratingValues['final_mark'] .'/100</b>';
                                
                                if ($ratingValues['date_last_edit'] != '0000-00-00')
                                {
                                    $title .=  '<i> ' . $translate->translate('editWhen') . ' ' . $ratingValues['date_last_edit'] . '</i>';
                                }
                                echo $title;
                            ?>
    	                </h4><br><br>
                        <table style="border-collapse: separate; border-spacing: 10px;">
                            <tr>
                                <th><?php echo $translate->translate('DateCall'); ?> : </th><td><?php echo $ratingValues['date_call'];?></td>
                                <th><?php echo $translate->translate('DateEvaluation'); ?> : </th><td><?php echo $ratingValues['date_eval'];?></td>
                                <th><?php echo $translate->translate('NAkio'); ?> : </th><td><?php echo $ratingValues['n_akio'];?></td>
                            </tr>
                            <tr>
                                <th><?php echo $translate->translate('NameRater'); ?> : </th><td><?php echo $ratingValues['name_rater'];?></td>
                                <th><?php echo $translate->translate('Consultant'); ?> : </th><td><?php echo $ratingValues['name_consultant'];?></td>
                                <?php if (isset($countryName['id'])) : ?>
                                    <th><?php echo $translate->translate('AdminNewAccountAccountCountry'); ?> : </th><td><?php echo $countryName['name']; ?></td>
                                <?php endif ?>
                            </tr>
                            <tr>
                                <?php if (isset($recipientName['id'])) : ?>
                                    <th><?php echo $translate->translate('Recipient'); ?> : </th><td><?php echo $recipientName['name']; ?></th>
                                <?php endif ?>
                                <?php if (isset($topic['id'])) : ?>
                                    <th><?php echo $translate->translate('NameTopic'); ?> : </th><td><?php echo $topic['description']; ?></td>
                                <?php endif ?>
                                <th><?php echo $translate->translate('EvaluationType'); ?> : </th><td><?php echo ucfirst($ratingValues['eval_type']);?></td>
                            </tr>
                            <tr>
                                <th><?php echo $translate->translate('team'); ?> : </th><td><?php echo $ratingValues['team']; ?></td>
                                <th><?php echo $translate->translate('Validation'); ?> : </th>
                                <?php 
                                    if ($ratingValues['validation'] == 1)
                                    {
                                        $validationStr = "Non";
                                    }
                                    else
                                    {
                                        $validationStr = "Oui";
                                    }
                                ?>
                                <td><?php echo $validationStr; ?></td>
                                <th><?php echo $translate->translate('AdminNewAccountAccountGroup'); ?> : </th><td><?php echo $ratingValues['groups'];?></td>
                            </tr>
                            <tr>
                                <th><?php echo $translate->translate('NOrder'); ?> : </th><td><?php echo $ratingValues['n_order'];?></td>
                                <th><?php echo $translate->translate('Improvement'); ?> : </th><td><?php echo $ratingValues['improvement'];?></td>
                                <th><?php echo $translate->translate('RatingName'); ?> : </th><td><?php echo $ratingValues['eval_name'];?></td>
                            </tr>
                        </table>
                        <div>
                            <br>
                            <b><?php echo $translate->translate('finalMark'); ?><b> : <?php echo $ratingValues['final_mark'];?><br>
                            <b><?php echo $translate->translate('optionalMark'); ?></b>  : <?php echo $ratingValues['optional_mark'];?>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <br><br>
                        <button class="btn btn-default" onclick="window.print();"><i class="fa fa-print"></i> Print</button>
                        <h4 class="box-title">
                            <?php echo $translate->translate('marking'); ?>
                        </h4>
                        <table class="table table-condensed">
                            <tbody>
                                <tr>
                                    <th><?php echo $translate->translate('NameCriteria'); ?></th>
                                    <th><?php echo $translate->translate('MarkCriteria'); ?></th>
                                    <th><?php echo $translate->translate('Commentary'); ?></th>
                                </tr>
                                <?php foreach ($criteria as $row) :?>
                                    <?php

                                        if ($row['set_to_zero'] == 1)
                                        {
                                            $set_to_zero = '<i class="fa fa-fw fa-exclamation"></i>' . $translate->translate('setToZero');
                                        }
                                        else 
                                        {
                                            $set_to_zero = '';
                                        }

                                        echo    '<tr>
                                                    <td>' . 
                                                        $row['name_criteria'] . ' ( '. $row['coefficient_criteria'] .' ) ' . $set_to_zero .
                                                    '</td>
                                                    <td>
                                                        <i onclick="showEdit('. $row['id_criteria'] .');" class="fa fa-pencil" style="cursor:pointer;"></i> ';
                                        if ($row['name_mark'] == 'non applicable')
                                        {
                                            echo            "<span id='mark_". $row['id_criteria'] ."'>" . $row['name_mark'] . "</span>";
                                        }
                                        else
                                        {
                                            echo             "<span id='mark_". $row['id_criteria'] ."'>" . $row['mark'] . ' ( ' . $row['name_mark'] . ' )' . "</span>";
                                        }
                                        echo        '</td>
                                                    <td id="comment_'. $row['id_criteria'] .'" class="col-md-7">' .
                                                        $row['commentary']
                                                    .'</td>
                                                </tr>'; 
                                    ?>
                                <?php endforeach ?>
                                <tr>
                                    <td>
                                        <b><?php echo $translate->translate('finalMark'); ?></b>
                                    </td>
                                    <td>
                                        <b id="final_mark"><?php echo $ratingValues['final_mark'];?></b>
                                        <div onclick="actualiser();" type="submit" class="btn btn-success" style="margin-left:10px;"><i class="fa fa-fw fa-refresh"></i></div>
                                    </td>
                                    <td>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <i><?php echo $translate->translate('markWONullDetail'); ?></i>
                                    </td>
                                    <td>
                                        <i><?php echo $ratingValues['optional_mark'];?></i>
                                    </td>
                                    <td>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
        		</div>
                <?php $account_type = $db->getAccountType($ratingValues['id_account_rater']);?>
                <?php if (isset($account_type[0]['id']) && $account_type[0]['id'] != 2 && $ratingValues['final_mark'] < 50 ) :?>
                    <div class="col-md-12 box box-info">
                        <div class="box-header">
                            <h3><?php echo $translate->translate('progressWorkflow') . ': ' . $ratingValues['progress'] . '%'; ?>
                                <span style="<?php echo (($ratingValues['reject_debrief'] == 1) ? 'color:red': 'color:green');?>">
                                    <?php 
                                        if ($ratingValues['progress'] == 100)
                                        {
                                            echo (($ratingValues['reject_debrief'] == 1) ? $translate->translate('debriefingRejected') : $translate->translate('debriefingAccepted'));    
                                        }
                                    ?>
                                </span>
                            </h3>
                            <div class="progress progress-striped">
                                <?php 
                                    $colorBar = ($ratingValues['reject_debrief'] == 1) ? 'progress-bar-danger' : 'progress-bar-success';
                                ?>
                                <div class="progress-bar <?php echo $colorBar; ?>" role="progressbar" aria-valuenow="<?php echo $ratingValues['progress']; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $ratingValues['progress']; ?>%">
                                    <span class="sr-only"><?php echo $ratingValues['progress']; ?>% Complete (success)</span>
                                </div>
                            </div>
                        </div>
                        <div class="box-body">
                            <form id="form_save_debrief" action="save_debriefing.php" method="post">
                                <input hidden type="text" name="id_rating"      value="<?php echo $_GET['id']; ?>">
                                <input hidden type="text" name="account_type"   value="<?php echo $session->id_accountType; ?>">
                                <h4><?php echo $translate->translate('finding'); ?> : </h4>
                                <textarea name="finding" class="form-control" rows="4"><?php 
                                        if (isset($debrief[0]['finding'])) 
                                        {
                                            echo $debrief[0]['finding'];
                                        }
                                    ?></textarea>
                                <h4><?php echo $translate->translate('diagnosis'); ?> : </h4>
                                <textarea name="diagnosis" class="form-control" rows="4"><?php 
                                        if (isset($debrief[0]['diagnosis'])) 
                                        {
                                            echo $debrief[0]['diagnosis'];
                                        }
                                    ?></textarea>
                                <h4><?php echo $translate->translate('back'); ?> : </h4>
                                <textarea name="back" class="form-control" rows="4"><?php 
                                        if (isset($debrief[0]['back'])) 
                                        {
                                            echo $debrief[0]['back'];
                                        }
                                    ?></textarea>
                                <h4><?php echo $translate->translate('actionPlan'); ?> : </h4>
                                <textarea name="action_plan" class="form-control" rows="4"><?php 
                                        if (isset($debrief[0]['action_plan'])) 
                                        {
                                            echo $debrief[0]['action_plan'];
                                        }
                                    ?></textarea>
                            </form>
                        </div>
                        <?php 
                            $groups_list = explode(';', $session->group);
                            foreach ($groups_list as $value)
                            {
                                $group_name = $db->getGroupNameByID($value);
                                if (isset($group_name[0]['name']))
                                {
                                    $groups_name_list[] = $group_name[0]['name'];
                                }
                            }

                            $isInGroup = in_array($ratingValues['groups'], $groups_name_list);
                        ?>
                        <?php if ($isInGroup && $ratingValues['progress'] != 100) :?>
                            <div class="box-footer">
                                <button id="send_debrief" type="submit" class="btn btn-primary"><?php echo $translate->translate('confirmDebriefing'); ?></button>
                            </div>
                        <?php endif; ?>
                        <?php if ($ratingValues['progress'] != 100) :?>
                            <?php if ($session->id_accountType == 1) : ?>
                                <div class="box-footer pull-right">
                                    <button id="validate_debrief" type="submit" class="btn btn-success"><?php echo $translate->translate('validateDebrief'); ?></button>
                                </div>
                                <div class="box-footer pull-right">
                                    <button id="deny_debrief" type="submit" class="btn btn-danger"><?php echo $translate->translate('denyDebrief'); ?></button>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                <?php endif; ?> 
            <?php endif; ?>
            <div id="editCriteria" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog text">
                    <form role="form" action="" method="post">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                <div id="titleName"></div>
                            </div>
                            <div class="modal-body">
                                <div id="content">
                                </div>
                            </div>
                            <div class="modal-footer">
                                <div onclick="saveModification();" type="submit" class="btn btn-danger"><?php echo $translate->translate('AdminModifySaveEmail'); ?></div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
	    </section>
	<?php endif ?>
</aside>
<?php require_once('footer.php'); ?>
<script>
    function actualiser()
    {
        location.reload(true);
    }

    function saveModification()
    {
        result = $('#newIdMark option:selected').attr('value').split('_');
        idCriteria = result[1];
        idMark = result[0];
        idRating = $('#id_rating').text();
        commentary = $('#commentary').val();
        finalMark = $('#final_mark').text();


        if (idCriteria == 0)
        {
            $("#newIdMark > option").each(function(){
                result = this.value.split('_');
                result = result[1];
                if (result != 0)
                {
                    idCriteria = result;
                    console.log(idCriteria);
                }
            });
        }
        console.log(idCriteria, idMark, idRating, commentary, finalMark);
        $.ajax({
            type: "GET",
            url: "edit_criteria.php",
            data: {
                    id_criteria : idCriteria, 
                    id_mark : idMark, 
                    id_rating : idRating,
                    com : commentary,
                    final_mark : finalMark
                }
        });
        $('#editCriteria').modal('hide');
    }

    function showEdit(idCriteria)
    {
        var mark    = $('#mark_' + idCriteria).text();
        var comment = $('#comment_' + idCriteria).text();
        
        mark = mark.split(' ( ');
        mark = mark[1];
        mark = mark.split(' )');
        mark = mark[0];

        console.log(mark);
        $('#editCriteria').find('#content').html('');
        $.ajax({
          type: "GET",
          url: "get_criteria_details.php",
          data: { id: idCriteria }
        })
          .success(function(result) {
            var id_criteria = 0;
            var title = "";

            decoded = JSON.parse(result);
            //console.log(decoded);
            for (key in decoded)
            {
                if (typeof decoded[key]['criteriaName'] == 'string')
                {
                    title = decoded[key]['criteriaName'];
                }
            }
            $('#editCriteria').find('#titleName').html('<h4>' + title + '</h4>');
            var newDiv = '<div><label>Notation :</label><br><select id="newIdMark" name="newIdMark"><option selected disabled>...</option>';
            for (key in decoded)
            {
                var selected = ((mark == decoded[key]['name']) ? ("selected") : (""))
                if (decoded[key]['id_criteria'] != 0)
                {
                    id_criteria = decoded[key]['id_criteria'];
                }

                newDiv += '<option value="'+ decoded[key]['id'] + '_'+ id_criteria +'" '+ selected +'>' + decoded[key]['mark'] + ' ( ' + decoded[key]['name'] + ' )</option>';
                if (decoded[key]['id_criteria'] != 0)
                {
                    id_criteria = decoded[key]['id_criteria'];
                }
            }

            newDiv += '</select><br><label>Commentaire</label><textarea id="commentary" class="form-control" rows="2">'+ comment +'</textarea></div>';
            
            $('#editCriteria').find('#content').html(newDiv);
          });
        $('#editCriteria').modal('show');
    }

    function centerModal() {
        $(this).css('display', 'block');
        var $dialog = $(this).find(".modal-dialog");
        var offset = ($(window).height() - $dialog.height()) / 2;
        $dialog.css("margin-top", offset);
    }

    $('#validate_debrief').on('click', function(){
        $('#form_save_debrief').append('<input hidden type="text" name="validate_debrief" value="true">');
        $('#form_save_debrief').submit();
    });

    $('#deny_debrief').on('click', function(){
        $('#form_save_debrief').append('<input hidden type="text" name="deny_debrief" value="true">');
        $('#form_save_debrief').submit();
    });

    $('#send_debrief').on('click', function(){
        $('#form_save_debrief').submit();
    });

    $('.modal').on('show.bs.modal', centerModal);
    $(window).on("resize", function(){
        $('.modal:visible').each(centerModal);
    });
</script>
